package cn.iocoder.yudao.module.chat.mq.consumer.gpt;

import cn.hutool.cache.Cache;
import cn.hutool.cache.CacheUtil;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.util.CharUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.security.core.LoginUser;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.chat.controller.app.gpt.vo.ChatMessageVO;
import cn.iocoder.yudao.module.chat.service.gpt.ChatHistoryService;
import cn.iocoder.yudao.module.gpt.api.dto.MQResponseMessageDTO;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.function.Consumer;
@Component
public class GptResponseConsumer implements Consumer<MQResponseMessageDTO> {
    @Resource
    private SimpMessagingTemplate simpMessagingTemplate;

    @Resource
    private ChatHistoryService chatHistoryService;

    @Override
    public void accept(MQResponseMessageDTO mqResponseMessageDTO) {
        String content = mqResponseMessageDTO.getContent();
        if(mqResponseMessageDTO.getContent().equals("[DONE]") || StrUtil.isBlank(mqResponseMessageDTO.getContent())){
            return;
        }
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        Assert.notNull(loginUser,"LoginUser will never be null,please check Authorization System");
        simpMessagingTemplate.convertAndSendToUser(String.valueOf(loginUser.getId()),"/queue/question",content);
    }
}
